package com.marvels.financial.loan.controller;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.loan.api.FinancialCheckBillApi;
import com.marvels.financial.loan.dto.CheckBillDTO;
import com.marvels.financial.loan.dto.EverydayStatisticsDTO;
/**
 * 对账管理
 * @author 刘琴
 * 2018年10月10日
 */
@RequestMapping("/financial/checkBill")
@RestController
public class FinancialCheckBillController {
	
	@Autowired
	private FinancialCheckBillApi checkBillApi;
	
	/**
	 * 查询回款总额
	 * @param loanId
	 * @return
	 */
	@RequestMapping("selectReturnTotalMoney")
	@ResponseBody
	public Double selectReturnTotalMoney(@RequestBody String loanIds){
		return checkBillApi.selectReturnTotalMoney(loanIds);
	}
	
	/**
	 * 每日统计
	 * @param dto
	 * @return
	 */
	@RequestMapping("everydayStatistics")
	@ResponseBody
	public MarvelsResultDTO everydayStatistics(@RequestBody CheckBillDTO dto){
		List<EverydayStatisticsDTO> list = null;
		try {
			list = checkBillApi.selectEverydayStatistics(dto);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>每日统计查询list的筛选FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[everydayStatistics] error: ",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),e.getMessage(),null);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), list);
	
	}
	
	
	@RequestMapping("/downLoadEverydayStatisticsExcel")
	public void excelEverydayStatisticsDownload(CheckBillDTO dto,HttpServletResponse response,HttpServletRequest request){
		BufferedOutputStream output = null;
		ByteArrayOutputStream byteArrayOutputStream=null;
		try {
			String fileName = "每日统计.xls";
			HSSFWorkbook wb = checkBillApi.findEverydayStatisticsExcelWorkbook(dto);
			byteArrayOutputStream = new ByteArrayOutputStream();
			wb.write(byteArrayOutputStream);
			byte[] bs = byteArrayOutputStream.toByteArray();
			byteArrayOutputStream.flush();
			byteArrayOutputStream.close();
			response.setContentType("application/octet-stream;charset=ISO8859-1");
			
			output = new BufferedOutputStream(response.getOutputStream());
			// 中文文件名必须转码为 ISO8859-1,否则为乱码
			String fileNameDown = new String(fileName.getBytes(), "ISO8859-1");
			// 作为附件下载
			response.setHeader("Content-Disposition", "attachment;filename=" + fileNameDown);
			
			output.write(bs);
			response.flushBuffer();
			output.close();
			
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>下载每日统计--查询列表异常:", e);
		}
		finally {
			if(byteArrayOutputStream!=null){
				try {
					byteArrayOutputStream.close();
				} catch (IOException e) {
					MarvelsLogUtil.getInstance().error(">>>>>>>>>关闭输出流异常byteArrayOutputStream", e);
				}
			}
			if (output != null)
				try {
					output.close();
				} catch (IOException e) {
					MarvelsLogUtil.getInstance().error(">>>>>>>>>关闭输出流异常output", e);
				}
		}
		
	}
}
